package com.julian.warehouse.activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.julian.warehouse.R;
import com.julian.warehouse.adapter.LvAdapter;
import com.julian.warehouse.base.SPBaseActivity;
import com.julian.warehouse.bean.GoodsBean;
import com.julian.warehouse.database.GoodsTable;
import com.julian.warehouse.database.MyDBOpenHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class PandianActivity extends SPBaseActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg;
    private TextView countAll;
    private TextView countRaw;
    private ListView lv;
    private List<GoodsBean> dataList;
    private SQLiteDatabase db;
    private LvAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pandian);
        super.init();
        setHeadTitle(true,"库存盘点");

    }


    @Override
    public void initView() {
         countAll = (TextView) findViewById(R.id.tv_count_all);
         countRaw = (TextView) findViewById(R.id.tv_count_raw);

        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);

        lv = (ListView) findViewById(R.id.lv);

    }

    @Override
    public void initData() {


        MyDBOpenHelper myDBOpenHelper = new MyDBOpenHelper(this);
        db = myDBOpenHelper.getReadableDatabase();

        dataList = new ArrayList<>();
        adapter = new LvAdapter(this, dataList);
        lv.setAdapter(adapter);
        queryGoodsAllCount();
        queryByColumeOrder(GoodsTable.COL_COUNT);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.btn_count:
                queryByColumeOrder(GoodsTable.COL_COUNT);
                break;
            case R.id.btn_ctime:
                queryByColumeOrder(GoodsTable.COL_CTIME);
                break;
            case R.id.btn_zero:
                queryByColumeOrder("");
                break;
            default:break;
        }

    }

    private void queryGoodsAllCount(){

        String sql = "select "+ GoodsTable.COL_COUNT+" from "+GoodsTable.TBL_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        int totalRows =  cursor.getCount();
        countRaw.setText(totalRows+"");
        int totalCount=0;

        while (cursor.moveToNext()){
            int count = cursor.getInt(cursor.getColumnIndex(GoodsTable.COL_COUNT));
            totalCount += count;
        }

        countAll.setText(totalCount+"");


    }


    private void queryByColumeOrder(String whatColumn){
        String sql;
        if (whatColumn.equals("")){
            sql = "select * from "+GoodsTable.TBL_NAME+" where "+GoodsTable.COL_COUNT+" = 0";
        }else if(whatColumn.equals(GoodsTable.COL_CTIME)){
            sql = "select * from "+GoodsTable.TBL_NAME+" order by CAST("+whatColumn+" as Datetime)";
        }else {
            sql = "select * from "+GoodsTable.TBL_NAME+" order by "+whatColumn;
        }


        Cursor cursor = db.rawQuery(sql, null);
        dataList.clear();
        while (cursor.moveToNext()){
            GoodsBean goodsBean = new GoodsBean();
            goodsBean.setGname(cursor.getString(cursor.getColumnIndex(GoodsTable.COL_GNAME)));
            goodsBean.setCtime(cursor.getString(cursor.getColumnIndex(GoodsTable.COL_CTIME)));
            goodsBean.setDescription(cursor.getString(cursor.getColumnIndex( GoodsTable.COL_DESCRIPTION)));
            goodsBean.setPicurl(cursor.getString(cursor.getColumnIndex(GoodsTable.COL_PICURL)));
            goodsBean.setCount(cursor.getInt(cursor.getColumnIndex(GoodsTable.COL_COUNT)));
            goodsBean.setPrice(cursor.getDouble(cursor.getColumnIndex(GoodsTable.COL_PRICE)));
            goodsBean.setCost(cursor.getDouble(cursor.getColumnIndex(GoodsTable.COL_COST)));
            goodsBean.setId(cursor.getInt(cursor.getColumnIndex(GoodsTable.COL_ID)));

            if (whatColumn.equals("")) {
                dataList.add(goodsBean);
            }else {
                if (goodsBean.getCount()!=0){
                    dataList.add(goodsBean);
                }
            }

        }

        if(whatColumn.equals(GoodsTable.COL_CTIME)){
            Collections.reverse(dataList);     //实现list集合逆序排列,让最新时间加入的产品在上面显示
        }

        adapter.notifyDataSetChanged();

    }







    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
